home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GLFLARE / LOADLUM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.0 KB  |  183 lines

  1.  
  2. /* texture.c - by David Blythe, SGI */
  3.  
  4. /* load_luminace is a simplistic routine for reading an SGI .bw image file. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include "loadlum.h"
  11.  
  12. typedef struct _ImageRec {
  13.   unsigned short imagic;
  14.   unsigned short type;
  15.   unsigned short dim;
  16.   unsigned short xsize, ysize, zsize;
  17.   unsigned int min, max;
  18.   unsigned int wasteBytes;
  19.   char name[80];
  20.   unsigned long colorMap;
  21.   FILE *file;
  22.   unsigned char *tmp;
  23.   unsigned long rleEnd;
  24.   unsigned int *rowStart;
  25.   int *rowSize;
  26. } ImageRec;
  27.  
  28. static void
  29. ConvertShort(unsigned short *array, unsigned int length)
  30. {
  31.   unsigned short b1, b2;
  32.   unsigned char *ptr;
  33.  
  34.   ptr = (unsigned char *) array;
  35.   while (length--) {
  36.     b1 = *ptr++;
  37.     b2 = *ptr++;
  38.     *array++ = (b1 << 8) | (b2);
  39.   }
  40. }
  41.  
  42. static void
  43. ConvertUint(unsigned *array, unsigned int length)
  44. {
  45.   unsigned int b1, b2, b3, b4;
  46.   unsigned char *ptr;
  47.  
  48.   ptr = (unsigned char *) array;
  49.   while (length--) {
  50.     b1 = *ptr++;
  51.     b2 = *ptr++;
  52.     b3 = *ptr++;
  53.     b4 = *ptr++;
  54.     *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
  55.   }
  56. }
  57.  
  58. static ImageRec *
  59. ImageOpen(char *fileName)
  60. {
  61.   union {
  62.     int testWord;
  63.     char testByte[4];
  64.   } endianTest;
  65.   ImageRec *image;
  66.   int swapFlag, x;
  67.  
  68.   endianTest.testWord = 1;
  69.   if (endianTest.testByte[0] == 1)
  70.     swapFlag = 1;
  71.   else
  72.     swapFlag = 0;
  73.  
  74.   image = (ImageRec *) malloc(sizeof(ImageRec));
  75.   if (image == NULL) {
  76.     fprintf(stderr, "Out of memory!\n");
  77.     exit(1);
  78.   }
  79.   if ((image->file = fopen(fileName, "rb")) == NULL) {
  80.     perror(fileName);
  81.     exit(1);
  82.   }
  83.   fread(image, 1, 12, image->file);
  84.  
  85.   if (swapFlag)
  86.     ConvertShort(&image->imagic, 6);
  87.   image->tmp = (unsigned char *) malloc(image->xsize * 256);
  88.   if (image->tmp == NULL) {
  89.     fprintf(stderr, "Out of memory!\n");
  90.     exit(1);
  91.   }
  92.   if ((image->type & 0xFF00) == 0x0100) {
  93.     x = image->ysize * image->zsize * (int) sizeof(unsigned);
  94.     image->rowStart = (unsigned *) malloc(x);
  95.     image->rowSize = (int *) malloc(x);
  96.     if (image->rowStart == NULL || image->rowSize == NULL) {
  97.       fprintf(stderr, "Out of memory!\n");
  98.       exit(1);
  99.     }
  100.     image->rleEnd = 512 + (2 * x);
  101.     fseek(image->file, 512, SEEK_SET);
  102.     fread(image->rowStart, 1, x, image->file);
  103.     fread(image->rowSize, 1, x, image->file);
  104.     if (swapFlag) {
  105.       ConvertUint(image->rowStart, x / (int) sizeof(unsigned));
  106.       ConvertUint((unsigned *) image->rowSize, x / (int) sizeof(int));
  107.     }
  108.   }
  109.   return image;
  110. }
  111.  
  112. static void
  113. ImageClose(ImageRec * image)
  114. {
  115.   fclose(image->file);
  116.   free(image->tmp);
  117.   free(image);
  118. }
  119.  
  120. static void
  121. ImageGetRow(ImageRec * image, unsigned char *buf, int y, int z)
  122. {
  123.   unsigned char *iPtr, *oPtr, pixel;
  124.   int count;
  125.  
  126.   if ((image->type & 0xFF00) == 0x0100) {
  127.     fseek(image->file, (long) image->rowStart[y + z * image->ysize], SEEK_SET);
  128.     fread(image->tmp, 1, (unsigned int) image->rowSize[y + z * image->ysize],
  129.       image->file);
  130.     iPtr = image->tmp;
  131.     oPtr = buf;
  132.     for (;;) {
  133.       pixel = *iPtr++;
  134.       count = (int) (pixel & 0x7F);
  135.       if (!count)
  136.         return;
  137.       if (pixel & 0x80) {
  138.         while (count--)
  139.           *oPtr++ = *iPtr++;
  140.       } else {
  141.         pixel = *iPtr++;
  142.         while (count--)
  143.           *oPtr++ = pixel;
  144.       }
  145.     }
  146.   } else {
  147.     fseek(image->file, 512 + (y * image->xsize) + (z * image->xsize * image->ysize),
  148.       SEEK_SET);
  149.     fread(buf, 1, image->xsize, image->file);
  150.   }
  151. }
  152.  
  153. unsigned char *
  154. load_luminance(char *name, int *width, int *height, int *components)
  155. {
  156.   unsigned char *base, *lptr;
  157.   ImageRec *image;
  158.   int y;
  159.  
  160.   image = ImageOpen(name);
  161.  
  162.   if (!image)
  163.     return NULL;
  164.   if (image->zsize != 1)
  165.     return NULL;
  166.  
  167.   *width = image->xsize;
  168.   *height = image->ysize;
  169.   *components = image->zsize;
  170.  
  171.   base = (unsigned char *)
  172.     malloc(image->xsize * image->ysize * sizeof(unsigned char));
  173.   if (!base)
  174.     return NULL;
  175.   lptr = base;
  176.   for (y = 0; y < image->ysize; y++) {
  177.     ImageGetRow(image, lptr, y, 0);
  178.     lptr += image->xsize;
  179.   }
  180.   ImageClose(image);
  181.   return base;
  182. }
  183.